package com.aeon.thumbnail.ui;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

public class ThumbnailControl extends Canvas {

	private Image mImage;
	private String mText=new String();
	private String toolTip;
	private boolean selected=false;

	public ThumbnailControl(Composite parent, int style) {
		super(parent, style);
		addMouseListener(new MouseListener() {
			
			@Override
			public void mouseUp(MouseEvent e) {
				selected=true;
				ThumbnailControl.this.redraw();
			}
			
			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		addTraverseListener(new TraverseListener() {

			@Override
			public void keyTraversed(TraverseEvent e) {

			}
		});
		this.addPaintListener(new PaintListener() {

			@Override
			public void paintControl(PaintEvent e) {
				onPaint(e);
			}
		});
	}

	public Image getImage() {
		return mImage;
	}

	public void setImage(Image mImage) {
		this.mImage = mImage;
	}

	public String getText() {
		return mText;
	}

	public void setText(String mText) {
		this.mText = mText;
	}

	public String getToolTip() {
		return toolTip;
	}

	public void setToolTip(String toolTip) {
		this.toolTip = toolTip;
	}

	@Override
	public void setSize(int width, int height) {
		super.setSize(width, height);
	}

	private void onPaint(PaintEvent e) {
		GC gc = e.gc;
		Rectangle rectangle=getClientArea();
		if(selected){
			gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
			gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
		}
		painImage(gc,(int)(rectangle.width*0.9),(int)(rectangle.height*0.6),(int)(rectangle.width*0.05),(int)(rectangle.height*0.05));
		paintText(gc,(int)(rectangle.width*0.05),(int)(rectangle.height*0.6));
		paintBorder(gc,getBounds());

	}

	

	private void paintText(GC gc, int i, int j) {
		
		gc.drawString(mText, i, j);
	}

	private void paintBorder(GC gc, Rectangle r) {
		Display disp = getDisplay();

		Color c1 = null;
		Color c2 = null;

		int style = getStyle();
		if ((style & SWT.SHADOW_IN) != 0) {
			c1 = disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
			c2 = disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW);
		}
		else if ((style & SWT.SHADOW_OUT) != 0) {
			c1 = disp.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW);
			c2 = disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
		}else{
			
			c1 = disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW);
			c2 = disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW);
		}

		if (c1 != null && c2 != null) {
			gc.setLineWidth(5);
			drawBevelRect(gc, r.x, r.y, r.width - 1, r.height - 1, c1, c2);
		}
	}

	private void drawBevelRect(GC gc, int x, int y, int w, int h,
			Color topleft, Color bottomright) {
		gc.setForeground(bottomright);
		gc.drawLine(x + w, y, x + w, y + h);
		gc.drawLine(x, y + h, x + w, y + h);

		gc.setForeground(topleft);
		gc.drawLine(x, y, x + w - 1, y);
		gc.drawLine(x, y, x, y + h - 1);
	}

	private void painImage(GC gc,int imgW,int imgH,int imgX,int imgY) {
		
		
		ImageData data = mImage.getImageData();
		Image image = ImageDescriptor.createFromImageData(
				data.scaledTo(imgW,imgH)).createImage();
		gc.drawImage(image, imgX, imgY);
	}
}
